package jcer.task;

import java.nio.ByteBuffer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jcer.core.*;

public class EchoTask extends Task {
	static Log logger = LogFactory.getLog(EchoTask.class);

	String msg;

	@Override
	public void decode(ByteBuffer buf) throws Exception {
		byte[] msgBuf = new byte[buf.remaining()];
		buf.get(msgBuf);
		msg = new String(msgBuf, "utf-8");
	}

	@Override
	public byte[] comsume() {
		try {
			logger.info(msg);
			String response = "EchoTask:" + msg;
			byte[] data = response.getBytes("utf-8");
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			return new byte[0];
		}
	}

}
